require 'rubygems'
gem 'sequel'

require 'database'

class DBMetalize
	attr_reader :dbConn, :metaTable
	def initialize(sequelConn, metaTableName='metajson')
		@dbConn = sequelConn
		@metaTable = metaTableName.to_s.to_sym
	end

	def createMetaTable
		@dbConn.create_table @metaTable do
			primary_key :id
			column :metatype, String, :null => false
			column :version, :integer, :null => false
			column :date, :timestamp
			column :metadata, :text
			index :metadata
		end
	end
	
	def dropMetaTable
		if (@dbConn.table_exists?@metaTable)
			@dbConn.drop_table(@metaTable)
		end
	end

	def convertTable(targetTable, excludeColumns=[])
		targetTable = targetTable.to_s.to_sym
		metaDataJson = jsonSchema(targetTable, excludeColumns)
		@dbConn[@metaTable].insert(:date => Time.now, :version => 1, :metadata => metaDataJson, :metatype => targetTable.to_s)
	end

	def jsonSchema(targetTable, excludeColumns=[])
		dbConn.schema_json(targetTable, excludeColumns)
	end
end

